class User {
  final String id;
  final String username;
  final String avatar;
  final String? bio;
  final String? email;
  final String? phone;
  final List<String> following; // 关注的用户ID列表
  final List<String> followers; // 粉丝ID列表
  final Map<String, dynamic>? statistics; // 统计数据

  User({
    required this.id,
    required this.username,
    required this.avatar,
    this.bio,
    this.email,
    this.phone,
    this.following = const [],
    this.followers = const [],
    this.statistics,
  });

  factory User.fromJson(Map<String, dynamic> json) {
    return User(
      id: json['id'],
      username: json['username'],
      avatar: json['avatar'],
      bio: json['bio'],
      email: json['email'],
      phone: json['phone'],
      following: List<String>.from(json['following'] ?? []),
      followers: List<String>.from(json['followers'] ?? []),
      statistics: json['statistics'],
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'username': username,
      'avatar': avatar,
      'bio': bio,
      'email': email,
      'phone': phone,
      'following': following,
      'followers': followers,
      'statistics': statistics,
    };
  }

  int getFollowingCount() {
    return following.length;
  }

  int getFollowersCount() {
    return followers.length;
  }

  int getPetsCount() {
    return statistics?['pets'] ?? 0;
  }

  int getPostsCount() {
    return statistics?['posts'] ?? 0;
  }

  int getVideosCount() {
    return statistics?['videos'] ?? 0;
  }

  User follow(String userId) {
    if (following.contains(userId)) {
      return this;
    }

    List<String> newFollowing = List.from(following);
    newFollowing.add(userId);

    return User(
      id: id,
      username: username,
      avatar: avatar,
      bio: bio,
      email: email,
      phone: phone,
      following: newFollowing,
      followers: followers,
      statistics: statistics,
    );
  }

  User unfollow(String userId) {
    if (!following.contains(userId)) {
      return this;
    }

    List<String> newFollowing = List.from(following);
    newFollowing.remove(userId);

    return User(
      id: id,
      username: username,
      avatar: avatar,
      bio: bio,
      email: email,
      phone: phone,
      following: newFollowing,
      followers: followers,
      statistics: statistics,
    );
  }

  bool isFollowing(String userId) {
    return following.contains(userId);
  }
} 